//
//  SearchDetailsViewController.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/7.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "SearchDetailsViewController.h"
#import "SearchViewController.h"
#import "SearchDetailsCollectionViewCell.h"
#import "HotDetailsViewController.h"
#import "NetworkHandler.h"
#import "MJRefresh.h"
#import "PresentDetailsModel.h"
#import "ToolDB.h"
#import "ToolModel.h"
#import "TAMacro.h"

@interface SearchDetailsViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong)NSMutableArray * array;
@property (nonatomic ,strong)SearchDetailsCollectionViewCell *celll;
@end

@implementation SearchDetailsViewController
- (NSMutableArray *)array {
    if (!_array) {
        self.array = [NSMutableArray array];
    }
    return _array;
}
- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.translucent=YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
    [butt setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    butt.frame  = CGRectMake(0, 0, 50, 50);
    UIBarButtonItem *left= [[UIBarButtonItem alloc]initWithCustomView:butt];
    [butt addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =left;
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:1.000 green:0.433 blue:0.523 alpha:1.000]];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.collectionView.mj_header beginRefreshing];
        [self mjRefresh];
    }];
    self.collectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self.collectionView.mj_footer beginRefreshing];
        [self mjRefreshAdd];
        [self.collectionView.mj_footer endRefreshing];
    }];
    [self foundCollectionView];
    [self mjRefresh];
    
}
- (void)mjRefresh {
    [NetworkHandler getDataByURLString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/search/item?sort=&limit=20&offset=0&keyword=%@",self.str]  BodyString:nil WithDataBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"items"];
        if (arr.count == 0) {
            UIImageView *image  = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            image.backgroundColor = [UIColor redColor];
            image.image = [UIImage imageNamed:@"67.jpg"];
            [self.view addSubview:image];
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 3  , SCREEN_HEIGHT / 10, SCREEN_WIDTH , SCREEN_HEIGHT / 10)];
            lable.text = @"暂无此物品";
            lable.font = [UIFont systemFontOfSize:25];
            lable.textColor = [UIColor whiteColor];
            [self.view addSubview:lable];
        }else{
            for (NSDictionary *dic in arr) {
                PresentDetailsModel *model = [PresentDetailsModel modelWithDic:dic];
                [self.array addObject:model];
                [self.collectionView reloadData];
            }
        }
    }];
}
- (void)mjRefreshAdd {
    
}
- (void)foundCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/2 - 14, SCREEN_HEIGHT /2.44);
    flowLayout.sectionInset = UIEdgeInsetsMake(0 , 5, 5, 5);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , SCREEN_HEIGHT ) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[SearchDetailsCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    [self.view addSubview:self.collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SearchDetailsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    cell.backgroundColor =  [UIColor whiteColor];
    PresentDetailsModel *model = self.array[indexPath.item];
    cell.presentDetailsModel = model;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HotDetailsViewController *HotDetails = [[HotDetailsViewController alloc]init];
    // 属性传值第3步
    PresentDetailsModel *model = self.array[indexPath.item];
    ToolModel *tool = [[ToolModel alloc]init];
    tool.title = model.name;
    tool.cover_image_url = model.cover_image_url;
    HotDetails.ID = [NSString stringWithFormat:@"%ld",(long)model.Id];
    tool.Id = HotDetails.ID;
    HotDetails.str = model.Id;
    HotDetails.IMAGE = model.cover_image_url;
    HotDetails.name = model.name;
    HotDetails.str = model.Id;
    [self.navigationController pushViewController:HotDetails animated:YES];
}
- (void)clickLeft:(UIBarButtonItem*)qwe {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
